//
//  BaseNetManager.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2018/12/13.
//  Copyright © 2018 wangws1990. All rights reserved.
//

import UIKit
import Alamofire
import Moya
class BaseNetManager: NSObject {
    class func hostUrl(txcode:String)->String{
        return "https://api.zhuishushenqi.com/"+(txcode);
    }
    class func iGetUrlString(urlString:String,parameters:[String:String],sucesss:@escaping ((_ object : AnyObject) ->Void),failure:@escaping ((_ error : String) ->Void))
    {
        
        Alamofire.request(urlString,method:.get,parameters:parameters, encoding: URLEncoding.default).responseJSON { (respond) in
            DispatchQueue.main.async {
                if respond.error != nil
                {
                    failure(respond.error.debugDescription);
                }else
                {
                    let json = respond.result.value;
                    sucesss (json as AnyObject);
                }
            }
        };
    }
    class func iPostUrlString(urlString:String,parameters:[String:String],sucesss:@escaping ((_ object : AnyObject) ->Void),failure:@escaping ((_ error : String) ->Void))
    {
        Alamofire.request(urlString,method:.post,parameters:parameters, encoding: JSONEncoding(options: [])).responseJSON { (respond) in
            DispatchQueue.main.async {
                if respond.error != nil
                {
                    failure(respond.error.debugDescription);
                }else
                {
                    let json = respond.result.value;
                    sucesss (json as AnyObject);
                }
            }
        };
    }
    class func postUrlString(urlString:String,parameters:[String:String],sucesss:@escaping ((_ object : AnyObject) ->()),failure:@escaping ((_ error : String) ->()))
    {
        let conifg : URLSessionConfiguration = URLSessionConfiguration.default;
        let session : URLSession = URLSession.init(configuration: conifg);
        var request = URLRequest.init(url: URL.init(string:urlString)!);
        request.httpMethod = "POST";
        request.timeoutInterval = 10;
        request.addValue("application/json", forHTTPHeaderField:"Content-Type")
        if JSONSerialization.isValidJSONObject(parameters) {
            do {
                let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
                request.httpBody = data;
            } catch  {
                
            }
        }
        let tast : URLSessionDataTask = session.dataTask(with: request) { (datas, respone, error) in
            DispatchQueue.main.async {
                do{
                    if error != nil
                    {
                        failure(error.debugDescription);
                    }else
                    {
                        let json = try JSONSerialization.jsonObject(with:datas!, options:.allowFragments) as! Dictionary<String, Any>
                        let dic = json["resultset"]
                        print(json);
                        let success = json["success"] as! Int
                        if Int(success) == 1{
                             sucesss(dic as AnyObject)
                        }else{
                            failure(json["err"] as! String);
                        }
                    }
                }catch{
                    
                }
            }
        }
        tast.resume();
    }
}
